package indi.zhifa.study2025.test.rabbitmq.nw.consumer;

import com.alibaba.fastjson2.JSON;
import com.rabbitmq.client.*;
import indi.zhifa.study2025.test.rabbitmq.common.Constant;
import indi.zhifa.study2025.test.rabbitmq.common.entity.OrderMsg;
import indi.zhifa.study2025.test.rabbitmq.common.utils.NWRabbitUtils;

import java.io.IOException;

public class NWConsumerApplication {

    public static void main(String[] args) throws IOException {
        Connection connection = null;
        try{
            connection = NWRabbitUtils.getConnection();
            if(null == connection){
                return;
            }
            Channel channel = connection.createChannel();
            // 消息预加载
            channel.basicQos(1);
            channel.basicConsume(Constant.NW_QUE_NAME,false,new DefaultConsumer(channel){

                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    String str = new String(body);
                    OrderMsg orderMsg = JSON.parseObject(str,OrderMsg.class);
                    System.out.println("接收到订单信息："+orderMsg);
                    channel.basicAck(envelope.getDeliveryTag(),false);
                }
            });

        }catch (Exception e){
            System.out.println(e);
        }
    }

}
